<?php
/***
 * 描述：商品价格管理
 * @category		Admin		
 * @package			Action			
 * @subpackage			
 * @author			wanght <whtaok@gmail.com>
 * @since			2013-4-6
 * @version			$Id: ProductPriceManageAction.class.php 48 2013-11-08 13:57:40Z whtaok@gmail.com $
 */
class ProductPriceManageAction extends AdminAction {
	
	/**
	 *
	 * @access		protected
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	protected  function _initialize() {
		parent::_initialize();
		$this->usingModel="ProductPrice";
	}
	
	/**
	 * 商品价格查询管理首页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function indexAct() {
		$this->showPricedListAct();
	}
	
	public function showPricedListAct(){
		$cateCode = I('cate_code');
		$search = array(
				'cate_code'=> I('cate_code'),
				'name'=>I('name'),
				'brand'=>I('brand'),
				'is_show'=>I('is_show','1',''),
		);
		//有商品运价的查询
		$viewMod = new ProductPriceViewModel();
		list($list,$page)=$viewMod->findListPage($this->curr_shop_id, $search);
		//dump($viewMod->_sql());
		$this->assign('list',$list);
		$this->assign('page',$page);
		$this->assign('search',$search);
		$this->assign('cateTree',productcate_tree());
		$this->display('list_priced');
	}
	/**
	 * 未定价商品查询管理页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function showUnpricedListAct() {
		$cateCode = I('cate_code');
		$search = array(
				'cate_code'=> I('cate_code'),
				'name'=>I('name'),
				'brand'=>I('brand'),
		);
		//有商品运价的查询
		$viewMod = new ProductPriceViewModel();
		list($list,$page)=$viewMod->findUnpricedListPage($this->curr_shop_id, $search);
		//dump($viewMod->_sql());
		$this->assign('list',$list);
		$this->assign('page',$page);
		$this->assign('search',$search);
		$this->assign('cateTree',productcate_tree());
		$this->display('list_unpriced');
	}
	/**
	 * 查询商品状态列表
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function showStatusListAct(){
		$cateCode = I('cate_code');
		$search = array(
				'cate_code'=> I('cate_code'),
				'name'=>I('name'),
				'brand'=>I('brand'),
				'is_show'=>1,
				'is_hot'=>I('is_hot'),
				'is_recommend'=>I('is_recommend'),
				'is_new'=>I('is_new'),
				'is_foreign'=>I('is_foreign'),
		);
		//有商品运价的查询
		$viewMod = new ProductPriceViewModel();
		list($list,$page)=$viewMod->findListPage($this->curr_shop_id, $search);
		$this->assign('list',$list);
		$this->assign('page',$page);
		$this->assign('search',$search);
		$this->assign('cateTree',productcate_tree());
		$this->display('list_status');
	}
	
	
	/**
	 * 更新指定店铺和商品ID的商品价格（实际价格，推荐价格，会员价）
	 * 该操作记录日志到商品变更表中
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doUpdPriceAct(){
		$shop_id = $this->curr_shop_id;
		$prod_id = I('prod_id');
		$price_real = I('price_real');
		$price_sale = I('price_sale');
		$price_vip = I('price_vip');
		$mod = new ProductPriceModel();
		$detail = $mod->updPrice($shop_id, $prod_id, $price_real, $price_sale, $price_vip);
		if($detail){
			$rtnData = array(
					'error'=>0,
					'message'=>'更新成功',
			);
		}else{
			$rtnData = array(
					'error'=>1,
					'message'=>$mod->getError(),
			);
		}
		$this->ajaxReturn($rtnData);

	
	}	
	
	
	/**
	 * 显示批量添加价格页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function showAddBatchAct(){
		if(is_array(I('id'))){
			$prod_id_list = array_values(I('id'));
		}else{
			$prod_id_list[] = I('id');
		}

		$viewMod = new ProductPriceViewModel();
		$viewMod->buildUnpricedFields($this->curr_shop_id);
		//显示可上架的商品，但是没有实际价格的商品列表
		$modWhere = array(
				'base_is_show'=>'1',
				'id'=>array('exp','is null')
		);
		if(count($prod_id_list)>0){
			$modWhere['prod_id']= array('in',$prod_id_list);
			$list = $viewMod->setModWhere($modWhere)->select();
			$this->assign('list',$list);
			$this->display('add_batch');
		}else{
			$this->error('请选择要设置价格/库存的商品');
		}
		
	}
	
	/**
	 * 批量添加价格
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doAddBatchAct(){
		$is_show = I('is_show');
		$prod_id = I('id');
		$price_real = I('price_real');
		$price_sale = I('price_sale');
		$price_vip = I('price_vip');
		$stock_qty = I('stock_qty');
		$stock_warn_qty = I('stock_warn_qty');
		$errors = '';
		foreach ($prod_id as $key=>$value){
			$mod=new ProductPriceModel();
			$data = array(
				'prod_id'=>$prod_id[$key],
				'price_real'=>$price_real[$key],
				'price_sale'=>$price_sale[$key],
				'price_vip'=>$price_vip[$key],
				'stock_qty'=>$stock_qty[$key],
				'stock_warn_qty'=>$stock_warn_qty[$key],
				'shop_id'=>$this->curr_shop_id,
			);
			if($mod->create($data)){
				$mod->is_show = $is_show;
				$lastId = $mod->add();
				if($lastId){
					continue;
				}else{
					$errors .= $value.':设置价格/库存时出现错误<br>';
				}
			}else{
				$errors .= $value.':'.$mod->getError().'<br>';
			}
		}
		if(strlen($errors)>0){
			$this->error($errors);
		}else{
			$this->success('价格/库存设置成功',U('showUnpricedList'));
		}
	
	}
	
	
	/**
	 * 显示批量更新价格页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function showUpdBatchAct(){
		if(is_array(I('id'))){
			$prod_id_list = array_values(I('id'));
		}else{
			$prod_id_list[] = I('id');
		}
	
		$viewMod = new ProductPriceViewModel();
		$viewMod->buildPricedFields($this->curr_shop_id);
		//显示可上架的商品，但是没有实际价格的商品列表
		$modWhere = array(
			'base_is_show'=>'1'
		);
		if(count($prod_id_list)>0){
			$modWhere['prod_id']= array('in',$prod_id_list);
			$list = $viewMod->setModWhere($modWhere)->select();
			$this->assign('list',$list);
			$this->display('upd_batch');
		}else{
			$this->error('请选择要设置价格/库存的商品');
		}
	
	}
	
	/**
	 * 批量添加运价
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doUpdBatchAct(){
		$is_show = I('is_show');
		$prod_id = I('id');
		$price_real = I('price_real');
		$price_sale = I('price_sale');
		$price_vip = I('price_vip');
		$stock_qty = I('stock_qty');
		$stock_warn_qty = I('stock_warn_qty');
		$errors = '';
		foreach ($prod_id as $key=>$value){
			$mod=new ProductPriceModel();
			$data = array(
					'prod_id'=>$prod_id[$key],
					'price_real'=>$price_real[$key],
					'price_sale'=>$price_sale[$key],
					'price_vip'=>$price_vip[$key],
					'stock_warn_qty'=>$stock_qty[$key],
					'stock_warn_qty'=>$stock_warn_qty[$key],
					'shop_id'=>$this->curr_shop_id,
			);
			if($mod->create($data)){
				$mod->is_show = $is_show;
				$modWhere = array(
					'shop_id'=>$this->curr_shop_id,
					'prod_id'=>$prod_id[$key],
				);
				$flag = $mod->setModWhere($modWhere)->save();
				if($flag !== false){
					continue;
				}else{
					$errors .= $value.':设置价格/库存时出现错误<br>';
				}
			}else{
				$errors .= $value.':'.$mod->getError().'<br>';
			}
		}
		if(strlen($errors)>0){
			$this->error($errors);
		}else{
			$this->success('价格/库存设置成功',U('showPricedList'));
		}
	
	}
	
	/**
	 * 显示编辑运价页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function showEditAct() {
		$prod_id = I('id');
		$mod = new ProductPriceModel();
		$modWhere['prod_id'] = $prod_id;
		$modWhere['shop_id'] = $this->curr_shop_id;
		$priceCnt = $mod->setModWhere($modWhere)->count();
		if($priceCnt == 0){
			$this->showAddBatchAct();
		}else if($priceCnt == 1){
			$this->showUpdBatchAct();
		}else{
			$this->error('商品价格/库存查询异常');
		}
	}
	
	/**
	 * 生成指定店铺的产品价格
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doAddAllAct(){
		$shop_id = $this->curr_shop_id;
		$user_id = $this->curr_user_id;
		$add_time = time();
		$productPriceFields = 'prod_id,price_real, price_sale,price_vip,stock_warn_qty,is_hot,is_recommend,ordlist,shop_id,crt_time,crt_user,upd_time,upd_user';
		$productFields = "id,price_sale,price_sale,price_vip,stock_warn_qty,is_hot,is_recommend,ordlist,$shop_id,$add_time,$user_id,$add_time,$user_id";
		$sql = "insert ignore product_price($productPriceFields) select $productFields from product_lib where is_valid in (1,2) and is_show=1 ";
		$mod = new Model();
		$flag = $mod->query($sql);
		if($flag === false){
			$this->error('执行时发生错误');
		}else{
			$this->success('执行成功');
		}
	}
	
	/**
	 * 刷新分类和品牌
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doRefreshBrandAct(){
		$shop_id = $this->curr_shop_id;
		$user_id = $this->curr_user_id;
		$savePath = C('REALPATH_SHOP').'/'.$shop_id.'/cate_brand';
		$add_time = time();
		$sql = "select cate_code2 ,brand,count(*) as item_cnt from product_price pp inner join product_lib pl on pp.prod_id=pl.id where pp.is_show=1 and shop_id=$shop_id group by cate_code2,brand ";
		$mod = new Model();
		$data = $mod->query($sql);
		$dataCache = array();
		foreach($data as $key=>$val){
			$dataCache[$val['cate_code2']][]=array($val['brand'],$val['item_cnt']);
		}
		F_CACHE($savePath,$dataCache);
		if(!$data){
			unset($data);
			unset($dataCache);
			$this->error('执行时发生错误');
		}else{
			unset($data);
			unset($dataCache);
			$this->success('执行成功');
		}
	}

	/**
	 * 删除操作
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doDelAct(){
		$id_list = array_values(I('id'));
		if($id_list){
			$mod = new ProductPriceModel();
			$data['prod_id'] = array('in',$id_list);
			$data['shop_id'] = $this->curr_shop_id;
			$mod->where($data)->delete();
		}
		$this->success('删除成功');
	}
}

?>